<template>
  <div class="alarm-info-maintain">
    <el-row style="paddingBottom: 0">
      <el-col :span="24">
        <dynForm :info-form="infoForm" :search-tags="searchTags" :select-list="selectList">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch">
            {{ $t("search") }}
          </el-button>
        </dynForm>
      </el-col>
    </el-row>
    <div class="table-buttons">
      <el-button
        v-btnPermission:edit
        :disabled="UpdateDisabled"
        type="primary"
        icon="el-icon-edit"
        @click="modify"
      >{{ $t("modify") }}</el-button>
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-upload"
        @click="doExport"
      >{{ $t("export") }}
      </el-button>
    </div>
    <el-card shadow="never">
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="getList"
      />
    </el-card>
    <infoMaintainForm ref="infoMaintainForm" @initList="initList"/>
  </div>
</template>

<script>
import mySetting from '@/mixins/mySetting'
import infoMaintainApi from '@/api/alarm/infoMaintain'
import { mapGetters } from 'vuex'
import { getEqpModelList } from '@/api/equipment/eqpbase'
import infoMaintainForm from './components/infoMaintainForm'
export default {
  name: 'AlarmInfoMaintain',
  components: { infoMaintainForm },
  mixins: [mySetting],
  data() {
    return {
      infoForm: {},
      searchTags: [
        { label: this.$t('eqpId'), prop: 'eqpId', tag: 'DynInput', labelwidth: '90px' },
        // { label: this.$t('eqpModel'), prop: 'eqpModel', tag: 'DynSelect', labelwidth: '90px' },
        { label: this.$t('AlarmCode'), prop: 'alarmId', tag: 'DynInput' },
        { label: this.$t('description'), prop: 'description', tag: 'DynInput' }
      ],
      selectList: {},
      tableColumns: Object.freeze([
        { prop: '', label: '', type: 'selection' },
        { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
          return (
            <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
          )
        } },
        { prop: 'eqpId', label: this.$t('eqpId') },
        // { prop: 'eqpModel', label: this.$t('eqpModel') },
        { prop: 'alarmID', label: this.$t('AlarmCode') },
        { prop: 'alarmDesc', label: this.$t('description'), width: '300px' },
        { prop: 'editTime', label: this.$t('editeTime') },
        // { prop: 'mtba', label: this.$t('MTBAOrNot'), templete: (_scope) => {
        //   return (
        //     <el-switch
        //       v-model={_scope.row.mtba}
        //       active-value={1}
        //       inactive-value={0}
        //       active-color={'#13ce66'}
        //       inactive-color={'#ff4949'}
        //       disabled/>
        //   )
        // } },
        { prop: 'alarmLevel', label: this.$t('AlarmLevel') },
        { prop: 'isEqpAlarm', label: this.$t('IsSelfAlarm'), templete: (_scope) => {
          return (
            <el-switch
              v-model={_scope.row.isEqpAlarm}
              active-value={1}
              inactive-value={0}
              active-color={'#13ce66'}
              inactive-color={'#ff4949'}
              disabled/>
          )
        } }
      ]),
      tableLoading: true
    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  created() {
    // this.getEqpModelsList()
    this.initList()
  },
  methods: {
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    async initList() {
      this.tableLoading = true
      const res = await infoMaintainApi.getList(this.$clearNull({ ...this.infoForm, ...this.listQuery }))
      this.tableData = res.data
      this.total = res.total
      this.tableLoading = false
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    },
    modify() {
      this.$refs.infoMaintainForm.handleOpen(this.multipleSelection[0].id)
    },
    getList(value) {
      this.listQuery = value
      this.initList()
    },
    async doExport() {
      const res = await infoMaintainApi.getList(this.$clearNull({
        ...this.infoForm,
        ...this.listQuery,
        toexcel: 1
      }))
      if (res.code === 1) {
        window.location.href = this.baseApi + '/' + res.data
      } else {
        this.doMerge(res)
      }
    },
    async getEqpModelsList() {
      const res = await getEqpModelList()
      this.$set(this.selectList, 'eqpModel', res.data)
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/defaultStyle.scss";
::v-deep .el-form {
  padding-top: 10px;
  // .el-form-item {
  //   margin-bottom: 15px;
  // }
}
</style>
